// app.js
import fly from './pages/http/index'
App({
  globalData:{
     // 自定义弹窗组件的配置参数
     comp_options:{}
  },
  setToastOption(data){
    let obj = {
      isShow:data.isShow || true , //全部
      isMask:data.isMask || true, //遮罩层
      message:data.message || '操作成功',
      icon:data.icon || 'success', // icon的类型，有效值：success, success_no_circle, info, warn, waiting, cancel, download, search, clear
      duration:data.durtion || 3000,
      color:`color: ${ 'data.color' || 'red' };`,
    }
    this.globalData.comp_options = obj
    console.log('修改app.js数据', data,this.globalData.comp_options ); 
  },
  // 
  onLaunch: async function () {
    // this.initObserve();
    // this.setToastOption({})
  },
  // 监听globalData中属性变化
initObserve() {
  const obj = this.globalData;
  const keys = ['comp_options'];
  keys.forEach(key => {
      let value = obj[key];
      obj[`${key}SubscriberList`] = [];
      Object.defineProperty(obj, key, {
          configurable: true,
          enumerable: true,
          set(newValue) {
              obj[`${key}SubscriberList`].forEach(watch => {
                  watch(newValue);
              });
              value = newValue;
          },
          get() {
              return value;
          }
      });
  });
},

// 订阅globalData中某个属性变化
subscribe(key, watch) {
watch(this.globalData[key]);
  this.globalData[`${key}SubscriberList`].push(watch);
},
  // 
  toast(mess,icon,duration){
    wx.showToast({
      icon:icon || 'success',
      title: mess || '成功',
      duration:duration || 3000,
    })
  },
  getData(api){
    return fly.get('http://127.0.0.1:8001'+api)
  },
  postData(api,data,header){
    if (api != undefined) {
      return fly.post('http://127.0.0.1:8001'+api,data,{header,})
    }
  },
  back(){
    wx.navigateBack({
      delta: 1,
    })
  },
  dialog(mess,boolean){
    wx.showModal({
      title:'系统消息',
      showCancel:boolean || false,
      content:mess || '操作成功',
      success: (res) => {
        if (res.cancel) {
          console.log(res);
            this.toast('取消成功','none')
        }
    
        if (res.confirm) {
            this.toast('登录成功','none')
        }
      }
    })
  }
})
